package cn.doctor.assistant.pad.ui.ziliao;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.ziliao.InformatApi;
import cn.doctor.assistant.pad.api.ziliao.RecordResponse;
import cn.doctor.assistant.pad.base.BaseFragment;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.Record;
import cn.doctor.assistant.pad.ui.UI;

/**
 * Created by jiaolf on 2016/3/30.
 * 病案资料列表
 */
public class RecordListFragment extends BaseFragment {

    @InjectView(R.id.layout_search)
    View layoutSearch;

    @InjectView(R.id.listview)
    ListView listview;
    @InjectView(R.id.text_info_bottom_count)
    TextView textInfoBottomCount;

    @InjectView(R.id.view_stub)
    ViewStub viewStub;
    @InjectView(R.id.text_info_bottom_total_page)
    TextView textInfoBottomTotalPage;
    @InjectView(R.id.text_info_bottom_current_page)
    TextView textInfoBottomCurrentPage;
    @InjectView(R.id.btn_bottom_previous_page)
    Button btnBottomPreviousPage;
    @InjectView(R.id.btn_bottom_next_page)
    Button btnBottomNextPage;
    @InjectView(R.id.edit_search)
    EditText editSearch;
    @InjectView(R.id.btn_search_clear)
    ImageButton btnSearchClear;
    @InjectView(R.id.btn_search)
    Button btnSearch;
    @InjectView(R.id.btn_add)
    Button btnAdd;
    @InjectView(R.id.listview_search)
    ListView listviewSearch;

    View rootView;

    List<Record> mRecords = new ArrayList<>();

    RecordListAdapter mAdapter;

    final int PAGESIZE = 20; // 每页显示个数

    int totalCount; // 总条数
    int totalPage;  // 总页数
    int currentPage = 1; // 当前页

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_info_list, container, false);
        ButterKnife.inject(this, rootView);
        viewStub.setLayoutResource(R.layout.layout_record_list_header);
        viewStub.inflate();
        layoutSearch.setVisibility(View.GONE);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize();
    }

    void initialize() {
        getRecords();

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UI.startActivity(ZhuYuanDetailActivity.class, "record", mRecords.get(position));
            }
        });
    }

    void getRecords() {
        showProgress("加载中...");
        InformatApi.getRecords(currentPage, PAGESIZE, new ResultCallback<RecordResponse>() {
            @Override
            public void onSuccess(RecordResponse response) {
                stopProgress();
                if (response.getRecords().size() > 0) {
                    mRecords.clear();
                    mRecords.addAll(response.getRecords());
                    Log.d("jzsn", Arrays.toString(mRecords.toArray()));

                    if (mAdapter == null) {
                        mAdapter = new RecordListAdapter(getActivity(), R.layout.list_item_record_info, mRecords);
                        listview.setAdapter(mAdapter);
                    } else {
                        mAdapter.notifyDataSetChanged();
                    }

                } else {
                    showToast("无数据");
                }

                totalCount = response.getTotalCount();
                totalPage = (totalCount + PAGESIZE - 1) / PAGESIZE;
                setBottomBarText();
            }
        });
    }

    void setBottomBarText() {
        // 总记录条数
        {
            String countStr = String.format("共有%d条记录", totalCount);
            SpannableStringBuilder ssb = new SpannableStringBuilder(countStr);
            ForegroundColorSpan span = new ForegroundColorSpan(getContext().getResources().getColor(R.color.assistant_theme_color));
            ssb.setSpan(span, 2, 2 + String.valueOf(totalCount).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textInfoBottomCount.setText(ssb);
        }

        // 总页数
        {
            String pageStr = String.format("共%d页", totalPage);
            SpannableStringBuilder ssb = new SpannableStringBuilder(pageStr);
            ForegroundColorSpan span1 = new ForegroundColorSpan(getContext().getResources().getColor(R.color.assistant_theme_color));
            int pageStrLength = String.valueOf(totalPage).length();
            ssb.setSpan(span1, 1, 1 + pageStrLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textInfoBottomTotalPage.setText(ssb);
        }

        // 当前页
        {
            String pageStr = String.format("当前第%d页", currentPage);
            SpannableStringBuilder ssb = new SpannableStringBuilder(pageStr);
            ForegroundColorSpan span1 = new ForegroundColorSpan(getContext().getResources().getColor(R.color.assistant_theme_color));
            int strLength = String.valueOf(currentPage).length();
            ssb.setSpan(span1, 3, 3 + strLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textInfoBottomCurrentPage.setText(ssb);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.btn_bottom_previous_page, R.id.btn_bottom_next_page})
    public void onClick(View view) {
        switch (view.getId()) {
            // 上一页
            case R.id.btn_bottom_previous_page:
                if (currentPage == 1) {
                    return;
                }
                currentPage--;
                getRecords();
                break;

            // 下一页
            case R.id.btn_bottom_next_page:
                if (currentPage == totalPage) {
                    return;
                }
                currentPage++;
                getRecords();
                break;
        }
    }
}
